<template>
<div class="articaldetail">
    <div class="header">
        <div class="left">
            <van-icon name="arrow-left back"
                      @click="$router.back()" />
            <span class="iconfont iconnew new"></span>
        </div>
        <span :class="{active:objNews.has_follow}" @click="followFn">{{objNews.has_follow?'已关注':'关注'}}</span>
    </div>
    <div class="detail">
        <div class="title">{{objNews.title}}</div>
        <div class="desc">
          <!-- nickname is undefined 因为是异步请求 -->
            <span>{{objNews.user&&objNews.user.nickname}}</span> &nbsp;&nbsp;
            <span>2019-9-9</span>
        </div>
        <div class="content" v-html="objNews.content.replace(this.pic,6002)" v-if="objNews.type==1"></div>
        <div class="content"  v-if="objNews.type==2">
          <video :poster="videoCover" :src="objNews.content.replace(9083,6002)" controls width="100%"></video>
        </div>
        <div class="opt">
            <span class="like" :class="{red:objNews.has_like}" @click="postLike">
                <van-icon name="good-job-o" />{{objNews.like_length>=0?objNews.like_length:'点赞'}}
            </span>
            <span class="chat">
                <van-icon name="chat"
                          class="w" />微信
            </span>
        </div>
    </div>
    <!-- 精彩跟帖 -->
    <div class="keeps">
        <h2>精彩跟帖</h2>
        <div class="item">
            <div class="head">
                <img src=""
                     alt />
                <div>
                    <p>火星网友</p>
                    <span>2小时前</span>
                </div>
                <span>回复</span>
            </div>
            <div class="text">文章说得很有道理</div>
        </div>
        <div class="more">更多跟帖</div>
    </div>
    <hmComment :newsObj='objNews'></hmComment>
</div>

</template>
<script>
import { newsDetail,post_like } from '@/apis/newsDetail'
import videoCover from '@/assets/reba.jpeg'
import { user_follows,user_unfollow} from '@/apis/user'
import hmComment from '@/components/hmComment.vue'
export default {
  components:{hmComment},
  created(){
    let id = this.$route.query.id
    // console.log(55,id);
    newsDetail(id).then(res=>{
      // console.log(58,res);
      this.objNews = res.data.data
      // console.log(60,this.objNews);
    })
  },
  data(){
    return{
      objNews:{},
      videoCover:videoCover,
      pic:/9083/g
    }
  },
  methods:{
    followFn(){
      //取消关注
      // if(this.objNews.has_follow===true){
      //   user_unfollow(this.objNews.user.id).then(res=>{
      //     this.$toast(res.data.message)
      //   if(res.data.message==='取消关注成功')
      //   {this.objNews.has_follow=false}
      //   })
      // }
      // //关注
      // if(this.objNews.has_follow===false){
      //   user_follows(this.objNews.user.id).then(res=>{
      //     this.$toast(res.data.message)
      //    if(res.data.message==='关注成功')
      //   {this.objNews.has_follow=true}
      //   })
      // }
      this.followF1(this.objNews,user_follows,user_unfollow)
    },
    //封装关注和未关注
    followF1(obj,follow,unfollow){
      let fn
      if(obj.has_follow===true){fn = unfollow}
      else{fn = follow}
      fn(obj.user.id).then(res=>{
          this.$toast(res.data.message)
        this.objNews.has_follow=!this.objNews.has_follow
        })
    },
    //点赞事件
    postLike(){
      post_like(this.objNews.id).then(res=>{
        console.log(105,res);
        this.objNews.has_like = !this.objNews.has_like
        this.objNews.has_like?this.objNews.like_length++:this.objNews.like_length--
        this.$toast(res.data.message)
      })
    }
  }

}
</script>


<style lang="less" scoped>
.header {
  padding: 0px 10px;
  height: 50px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #ccc;
  > .left {
    height: 100%;
    vertical-align: middle;
    position: relative;
    .van-icon {
      position: absolute;
      top: 15px;
      left: 0;
    }
    > span {
      margin-left: 22px;
      font-size: 50px;
    }
  }
  > span {
    padding: 5px 15px;
    background-color: #f00;
    color: #fff;
    text-align: center;
    border-radius: 15px;
    font-size: 13px;
    &.active{
      background-color: #fff;
      border: 1px solid #ccc;
      color: #000;
    }
  }
}
.detail {
  padding: 15px;
  .title {
    font-size: 18px;
    font-weight: bold;
    padding: 10px 0;
  }
  .desc {
    line-height: 30px;
    color: #999;
    font-size: 13px;
  }
  .content {
    text-indent: 2em;
    line-height: 24px;
    font-size: 15px;
    padding-bottom: 30px;
    width: 100%;
    /deep/ img{
      width: 100%;
      margin-left: -32px;
    }
    /deep/ video{
      width: 100%;
      margin-left: -32px;
    }
  }
}
.opt {
  display: flex;
  justify-content: space-around;
  .like,
  .chat {
    height: 25px;
    padding: 0 15px;
    font-size: 14px;
    line-height: 25px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
  }
  .w {
    color: rgb(84, 163, 5);
  }
  .red{
    color:red
  }
}
.keeps {
  border-top: 5px solid #ddd;
  padding: 0 15px;
  > h2 {
    line-height: 50px;
    text-align: center;
  }
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: (50/360) * 100vw;
        height: (50/360) * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
  .more {
    width: 100px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    border: 1px solid #ccc;
    border-radius: 15px;
    margin: 20px auto;
    font-size: 13px;
  }
}
</style>